﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DigDes.DSchool.SUPS.Web.UI
{
    public partial class CatalogCargo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (GridView1.SelectedIndex == -1)
            {
                _saveBtn.Visible = false;
                _cancelBtn.Visible = false;
                DetailsView1.Visible = false;
                _editBtn.Enabled = false;
                _deleteBtn.Enabled = false;
                _ImageLbl.Text = "Выберите груз";
            }
        }

        protected void _addBtn_Click(object sender, EventArgs e)
        {
            _stateLbl.Text = "Добавление:";
            DetailsView1.ChangeMode(DetailsViewMode.Insert);
            ModeState();
        }

        protected void _editBtn_Click(object sender, EventArgs e)
        {
            DetailsView1.ChangeMode(DetailsViewMode.Edit);
            _stateLbl.Text = "Редактирование: " + GridView1.Rows[GridView1.SelectedIndex].Cells[2].Text;
            ModeState();
        }

        protected void _deleteBtn_Click(object sender, EventArgs e)
        {
            _stateLbl.Text = "Удаление: " + GridView1.Rows[GridView1.SelectedIndex].Cells[2].Text;
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
            ModeState();
            _fileUploadLabel.Visible = false;
            FileUpload1.Visible = false;
        }

        void UploadImage(string ImageName)
        {
            if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "" && ImageName != "Выберите файл!" && ImageName != "Выберите груз!")
            {
                _ImageLbl.Text = "Изображение груза добавлено успешно! Выберите новый груз.";
                byte[] imageSize = new byte[FileUpload1.PostedFile.ContentLength];
                HttpPostedFile uploadedImage = FileUpload1.PostedFile;
                uploadedImage.InputStream.Read(imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);
                DigDes.DSchool.SUPS.DataAccess.Database.CargoDAC.UploadImageByName(ImageName, imageSize);
            }
            else
                _ImageLbl.Text = "Выберите груз";
        }

        protected void _saveBtn_Click(object sender, EventArgs e)
        {
            switch (DetailsView1.CurrentMode)
            {
                case DetailsViewMode.Edit:
                    {
                        CargoDetailODS.UpdateParameters["CargoName"].DefaultValue = ((TextBox)DetailsView1.FindControl("EditCargoNameTB")).Text;
                        CargoDetailODS.UpdateParameters["Description"].DefaultValue = ((TextBox)DetailsView1.FindControl("EditDescriptionTB")).Text;
                        CargoDetailODS.UpdateParameters["Code"].DefaultValue = ((TextBox)DetailsView1.FindControl("EditCodeTB")).Text;
                        CargoDetailODS.UpdateParameters["CodeETSNG"].DefaultValue = ((TextBox)DetailsView1.FindControl("EditCodeETSNGTB")).Text;
                        CargoDetailODS.UpdateParameters["CargoETSNGName"].DefaultValue = ((TextBox)DetailsView1.FindControl("EditCargoETSNGNameTB")).Text;
                        CargoDetailODS.UpdateParameters["CodeGNG"].DefaultValue = ((TextBox)DetailsView1.FindControl("EditCodeGNGTB")).Text;
                        CargoDetailODS.UpdateParameters["CodeGNGName"].DefaultValue = ((TextBox)DetailsView1.FindControl("EditCodeGNGNameTB")).Text;
                        CargoDetailODS.UpdateParameters["Mnemocode"].DefaultValue = ((TextBox)DetailsView1.FindControl("EditMnemocodeTB")).Text;
                        CargoDetailODS.Update();
                        GridView1.DataBind();
                        break;
                    }
                case DetailsViewMode.Insert:
                    {
                        CargoDetailODS.InsertParameters["CargoName"].DefaultValue = ((TextBox)DetailsView1.FindControl("InsertCargoNameTB")).Text;
                        CargoDetailODS.InsertParameters["Description"].DefaultValue = ((TextBox)DetailsView1.FindControl("InsertDescriptionTB")).Text;
                        CargoDetailODS.InsertParameters["Code"].DefaultValue = ((TextBox)DetailsView1.FindControl("InsertCodeTB")).Text;
                        CargoDetailODS.InsertParameters["CodeETSNG"].DefaultValue = ((TextBox)DetailsView1.FindControl("InsertCodeETSNGTB")).Text;
                        CargoDetailODS.InsertParameters["CargoETSNGName"].DefaultValue = ((TextBox)DetailsView1.FindControl("InsertCargoETSNGNameTB")).Text;
                        CargoDetailODS.InsertParameters["CodeGNG"].DefaultValue = ((TextBox)DetailsView1.FindControl("InsertCodeGNGTB")).Text;
                        CargoDetailODS.InsertParameters["CodeGNGName"].DefaultValue = ((TextBox)DetailsView1.FindControl("InsertCodeGNGNameTB")).Text;
                        CargoDetailODS.InsertParameters["Mnemocode"].DefaultValue = ((TextBox)DetailsView1.FindControl("InsertMnemocodeTB")).Text;
                        CargoDetailODS.Insert();
                        GridView1.DataBind();
                        break;
                    }
                case DetailsViewMode.ReadOnly:
                    {
                        CargoDetailODS.DeleteParameters["CodeGNG"].DefaultValue = ((Label)DetailsView1.FindControl("CodeGNGLbl")).Text;
                        CargoDetailODS.Delete();
                        GridView1.DataBind();
                        break;
                    }
            }

            NoModeState();
        }

        protected void _cancelBtn_Click(object sender, EventArgs e)
        {
            NoModeState();
            if (GridView1.SelectedIndex!=-1)
                GridView1.Rows[GridView1.SelectedIndex].BackColor = System.Drawing.Color.White;
            _ImageLbl.Text = "Выберите груз";
            _uploadBtn.Enabled = true;
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            _stateLbl.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[2].Text;
            _ImageLbl.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[2].Text;
            _uploadBtn.Enabled = true;
            GridView1.Rows[GridView1.SelectedIndex].BackColor = System.Drawing.Color.FromArgb(201, 224, 239);
            DetailsView1.Visible = true;
            _editBtn.Enabled = true;
            _deleteBtn.Enabled = true;
        }

        protected void NoModeState()
        {
            _stateLbl.Text = "";
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
            DetailsView1.Visible = false;
            _editBtn.Enabled = false;
            _deleteBtn.Enabled = false;
            _saveBtn.Visible = false;
            _cancelBtn.Visible = false;
            _addBtn.Enabled = true;
 
        }

        protected void ModeState()
        {
            DetailsView1.Visible = true;
            _saveBtn.Visible = true;
            _cancelBtn.Visible = true;
            _addBtn.Enabled = false;
            _editBtn.Enabled = false;
            _deleteBtn.Enabled = false;
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            if (GridView1.SelectedIndex != -1)
                GridView1.Rows[GridView1.SelectedIndex].BackColor = System.Drawing.Color.White;
            GridView1.DataBind();
        }

        protected void CargoODS_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            NoModeState();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           /* foreach (TableCell myCell in e.Row.Cells)
            {
                myCell.Style.Add("word-break", "break-all");
                myCell.Width = 40;
            }*/
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
                UploadImage(_ImageLbl.Text);
            else
                _ImageLbl.Text = "Выберите файл!";          
        }

    }
}